package org.hefan.pay.service.impl;

import org.hefan.basic.exception.BusinessException;
import org.hefan.basic.service.impl.BaseServiceImpl;
import org.hefan.pay.domain.AlipayInfo;
import org.hefan.pay.domain.PayBill;
import org.hefan.pay.mapper.AlipayInfoMapper;
import org.hefan.pay.mapper.PayBillMapper;
import org.hefan.pay.service.IPayBillService;
import org.hefan.pay.utils.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Override
    public String payRequest(PayBill payBillTmp) {
        if (payBillTmp == null) {
            throw new BusinessException("你的支付单不存在，请重新创建再支付!!!!!");
        }
        PayBill payBill = payBillMapper.loadByUnionPaySn(payBillTmp.getUnionPaySn());
        if (payBill == null) {
            throw new BusinessException("你的支付单不存在，请重新创建再支付!!!");
        }
        switch (payBill.getPayChannel().intValue()) {
            case 1: // 支付宝
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(payBillTmp.getShop_id());
                String payData = AlipayUtil.pay(alipayInfo, payBill);
                return payData;
            case 0: //余额
                System.out.println("余额");
                break;
            case 2: //微信
                System.out.println("微信");
                break;
            case 3: //银联
                System.out.println("银联");
                break;
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
